package lia.org.service;

import java.util.List;

import lia.org.dao.CarDao;
import lia.org.dao.MarcaDao;
import lia.org.dao.ModeloDao;
import lia.org.model.Car;
import lia.org.model.Filter;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.googlecode.objectify.Query;

@Service
public class CarService {

	@Autowired
	private CarDao carDao;	
	@Autowired
	private MarcaDao marcaDao;
	@Autowired
	private ModeloDao modeloDao;
	
	

	public MarcaDao getMarcaDao() {
		return marcaDao;
	}


	public ModeloDao getModeloDao() {
		return modeloDao;
	}


	public CarDao getCarDao() {
		return carDao;
	}

	
	public void add(Car car) {	
		this.carDao.add(car);	
	}

	public Car getById(String vin) {
		return this.carDao.getByProperty("vin", vin);
	}

	public void deleteById(Car car) {
		this.carDao.delete(car);
	}

	public List<Car> findAll(String filter) {
		Query<Car> q = this.carDao.listAll();

		return q.list();
	}

	public List<Car> findAllPag(int limit, int start, List<Filter> filters) {
		
		Query<Car> q = this.carDao.listAll(limit, start).				
				order("modelo.marca.name").
				order("modelo.name").
				order("precio").
				order("color");
		
		for (Filter filter : filters) {
			q.filter(filter.getProperty(), filter.getValue());
		}		
		
		return q.list();
	}

}
